package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.product.api.domain.ProductSpec;
import com.spzx.product.service.ProductSpecService;
import com.spzx.product.service.impl.CategoryBrandServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/spec")
@Tag(name = "商品规格属性后台管理模块")
public class ProductSpecController extends BaseController {
    @Resource
    private ProductSpecService productSpecService;
    @Resource
    private CategoryBrandServiceImpl categoryBrandService;

    @Operation(summary = "分页条件获取商品规格属性列表")
    @GetMapping("/list")
    @RequiresPermissions("product:spec:list")
    public TableDataInfo specList(ProductSpec productSpec) {
        startPage();
        List<ProductSpec> productSpecList = productSpecService.getSpecList(productSpec);
        return getDataTable(productSpecList);
        /*Page<ProductSpec> page = productSpecService.getSpecList(productSpec);
        return getDataTable(page);*/
    }

    @Operation(summary = "根据第3级分类id获取商品规格属性列表")
    @GetMapping("/listAll/{category3Id}")
    @RequiresPermissions("product:spec:listC3Id")
    public AjaxResult specCategory3IdList(@PathVariable Long category3Id) {
      return success(productSpecService.list(new LambdaQueryWrapper<ProductSpec>().eq(ProductSpec::getCategoryId, category3Id)));
    }

    @Operation(summary = "根据id查询商品规格属性详情")
    @GetMapping("{id}")
    @RequiresPermissions("product:spec:info")
    public AjaxResult specDetail(@PathVariable Long id) {
        ProductSpec productSpec = productSpecService.getById(id);
        List<Long> categoryIdList = categoryBrandService.getAllParentIds(productSpec.getCategoryId(), new ArrayList<Long>());
        Collections.reverse(categoryIdList);
        productSpec.setCategoryIdList(categoryIdList);
        return success(productSpec);
    }


    @Operation(summary = "根据ids批量删除商品规格属性")
    @DeleteMapping("{ids}")
    @RequiresPermissions("product:spec:remove")
    public AjaxResult deleteSpec(@PathVariable List<Long> ids) {
        return toAjax(productSpecService.removeBatchByIds(ids));
    }


    @Operation(summary = "新增|修改商品规格属性")
    @PostMapping("/saveOrUpdate")
    @RequiresPermissions("product:spec:saveOrUpdate")
    public AjaxResult saveOrUpdate(@Validated @RequestBody ProductSpec productSpec) {
        productSpecService.checkSpecUnique(productSpec);
        boolean flag = productSpecService.saveOrUpdate(productSpec);
        return toAjax(flag);
    }
}

